<template>
  <div>
    <el-form
      :model="postForm"
      ref="postForm"
      :rules="rules"
      label-width="120px"
      label-suffix=":"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          保存
        </el-button>
      </sticky>

      <div style="padding: 15px">
        <el-row>
          <el-col :span="22">
            <el-form-item label="注册需要推荐人">
              <el-switch v-model="postForm.register_referrer" />
            </el-form-item>
            <el-form-item label="下单需要推荐人">
              <el-switch v-model="postForm.order_referrer" />
            </el-form-item>
            <el-form-item label="推荐商品数量" style="width: 200px">
              <el-input v-model="postForm.recommend_num" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import api from '@/api'
export default {
  name: 'SystemFlow',
  components: {
    Sticky
  },
  data() {
    return {
      postForm: {
        status: 'draft'
      },
      rules: {
      },
      loading: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const { data } = await api.system.fetchFlow()
      this.postForm = Object.assign({}, data)
      this.postForm.status = 'publisher'
      this.postForm.register_referrer = +this.postForm.register_referrer === 1
      this.postForm.order_referrer = +this.postForm.order_referrer === 1
    },
    submitForm() {
      this.$refs.postForm.validate(validate => {
        if (validate) {
          this.loading = true
          api.system.saveFlow({
            ...this.postForm,
            register_referrer: this.postForm.register_referrer ? 1 : 0,
            order_referrer: this.postForm.order_referrer ? 1 : 0
          }).then(response => {
            this.$notify({
              title: '成功',
              message: response.message || '保存成功',
              type: 'success',
              duration: 2000
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
